package com.vitabuybuy.tv.adaptebinding

import android.graphics.Color
import android.graphics.Paint
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.databinding.BindingAdapter
import com.library.common.widget.BGButton
import com.vitabuybuy.tv.R
import java.math.BigDecimal

/**
 * 作者：SimGa
 * 日期：2021/3/8
 * 时间：16:49
 * 包名：com.library.common.core.adapter
 */

object TextAdapter {
    @BindingAdapter(value = ["status"])
    @JvmStatic
    fun setTextColor(textView: TextView, status: Int) {
        when (status) {
            1 -> {
                textView.setTextColor(Color.parseColor("#049F9E"))
            }
            2 -> {
                textView.setTextColor(Color.parseColor("#F46460"))
            }
            3 -> {
                textView.setTextColor(Color.parseColor("#F6955D"))
            }
            else -> {
                textView.setTextColor(Color.parseColor("#F6955D"))
            }
        }
    }
    @BindingAdapter(value = ["integer", "price", "postageString"], requireAll = false)
    @JvmStatic
    fun setTextValue(textView: TextView, integer: Int, price: String?, postageString: String?) {
        var amount = BigDecimal(0)
        if (price != null && price != "") {//如果金额不为0或者不会控
            amount = amount.add(BigDecimal(price))
        }
        if (postageString != null && postageString != "") {//如果运费不为0或者不会空
            amount = amount.add(BigDecimal(postageString))
        }
        if (amount.compareTo(BigDecimal(0)) == 0) {
            textView.text = integer.toString() + "积分"
        } else {
            textView.text =
                integer.toString() + "积分+" + amount.setScale(2, BigDecimal.ROUND_HALF_UP)
                    .stripTrailingZeros().toPlainString() + "元"
        }
    }
    //    待支付  non-payment
//    代发货 paid
//    待收货 shipped
//    待评价 finished
//    已取消 canceled
//    已撤销 rejected
    @BindingAdapter(value = ["status"], requireAll = false)
    @JvmStatic
    fun setStatusValue(textView: TextView, status: String?) {
        textView.apply {
            when (status) {
                "non-payment" -> {
                    textView.text = (textView.context).getString(R.string.wait_to_pay)
                }
                "paid" -> {
                    textView.text = (textView.context).getString(R.string.wait_to_deliver)
                }
                "shipped" -> {
                    textView.text = (textView.context).getString(R.string.wait_to_receive)
                }
                "finished" -> {
                    textView.text = (textView.context).getString(R.string.wait_to_comment)
                }
                "canceled" -> {
                    textView.text = (textView.context).getString(R.string.order_has_cancel)
                }
                "rejected" -> {
//                    textView.text = (textView.context).getString(R.string.wait_to_pay)
                }
                "close" ->{
                    textView.text = (textView.context).getString(R.string.order_has_completed)
                }
                "pending" ->{
                    textView.text = (textView.context).getString(R.string.wait_open_group)
                }
            }
        }
    }

    @BindingAdapter(value = ["projStatus"], requireAll = false)
    @JvmStatic
    fun setButtonValue(button: BGButton, projStatus: String?) {
        button.apply {
            when (projStatus) {
                "1" -> {
                    setNormalSolid(Color.parseColor("#33F46360"))
                    setTextColor(Color.parseColor("#F46360"))
                    text = "已开业"
                }
                "2" -> {
                    setNormalSolid(Color.parseColor("#33F6955D"))
                    setTextColor(Color.parseColor("#F6955D"))
                    text = "装修中"
                }
                "3" -> {
                    setNormalSolid(Color.parseColor("#333BB5B5"))
                    setTextColor(Color.parseColor("#3BB5B5"))
                    text = "试运营"
                }
            }
        }
    }

    @BindingAdapter(value = ["orderStatus"], requireAll = false)
    @JvmStatic
    fun setorderStatus(textView: TextView, orderStatus: Int) {
        textView.apply {
            when (orderStatus) {
                1 -> {
                    text = "待付款"
                }
                2 -> {
                    text = "待发货"
                }
                3 -> {
                    text = "已发货"
                }
                8 -> {
                    text = "已完成"
                }
                5 -> {
                    text = "已取消"
                }
            }
        }
    }

    @BindingAdapter(value = ["priceStr"], requireAll = false)
    @JvmStatic
    fun setNowPrice(textView: TextView, priceStr: Double) {
        textView.text =
            String.format(textView.context.getString(R.string.price_with_value), priceStr)
    }

    @BindingAdapter(value = ["oldPriceStr"], requireAll = false)
    @JvmStatic
    fun setOldPrice(textView: TextView, oldPriceStr: Double) {
        textView.apply {
            paint.flags = Paint.STRIKE_THRU_TEXT_FLAG or Paint.ANTI_ALIAS_FLAG // 设置中划线并加清晰
            text = String.format(textView.context.getString(R.string.price_with_value), oldPriceStr)
        }
    }

    @BindingAdapter(value = ["quantityStr"], requireAll = false)
    @JvmStatic
    fun setQuantityTextView(textView: TextView, quantityStr: Int) {
        textView.text = String.format(textView.context.getString(R.string.sold), quantityStr)
    }

    @BindingAdapter(value = ["totalAmount"], requireAll = false)
    @JvmStatic
    fun setTotalAmountTextView(textView: TextView, totalAmount: Double) {
        textView.text = String.format(textView.context.getString(R.string.total_price), totalAmount)
    }

    @BindingAdapter(value = ["stock"], requireAll = false)
    @JvmStatic
    fun setStockTextView(textView: TextView, stock: Int) {
        textView.text = String.format(textView.context.getString(R.string.stock), stock)
    }


    @BindingAdapter(value = ["couponStatus"],requireAll = false)
    @JvmStatic
    fun setCouponsTextView(textView: TextView, couponStatus: String) {
        textView.text = if(couponStatus == "未使用") textView.context.getText(R.string.use_now) else couponStatus
        when(couponStatus){
            "过期"->{
                textView.setTextColor(ContextCompat.getColor(textView.context,R.color.text_c0c4cc))
                textView.isEnabled = false
                textView.background = ContextCompat.getDrawable(textView.context,R.drawable.rect_14_all_none_gray)
            }
            "已使用"->{
                textView.setTextColor(ContextCompat.getColor(textView.context,R.color.text_c0c4cc))
                textView.isEnabled = false
                textView.background = ContextCompat.getDrawable(textView.context,R.drawable.rect_14_all_none_gray)
            }
            "未使用"->{
                textView.setTextColor(ContextCompat.getColor(textView.context,R.color.mainColor))
                textView.isEnabled = true
                textView.background = ContextCompat.getDrawable(textView.context,R.drawable.rect_14_all_none_red)
            }
        }
    }
}